package site.zhouinfo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet方法被调用");
        // 业务代码写在doGet或者doPost方法中
        //获得往客户端写的输出流
        PrintWriter out = response.getWriter();
        //向客户端写一个html页面
        out.print("<html>");
        out.print("<body>");
        out.print("<h3>");
        out.print("Hello World!");
        out.print("</h3>");
        out.print("</body>");
        out.print("</html>");
        out.flush();  //清空缓存
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost方法被调用");
        doGet(request, response);   //一般，doGet和doPost互相调用
    }

    @Override
    public void destroy() {
        System.out.println("destroy方法被调用");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("init方法被调用");
    }
}
